package com.gdssoft.system;

import javax.annotation.Resource;

import junit.framework.TestCase;

import org.junit.Test;
import org.springframework.test.annotation.Rollback;

import com.gdssoft.BaseTestCase;
import com.gdssoft.core.Constants;
import com.gdssoft.core.exception.ExistException;
import com.gdssoft.core.exception.NotCompleteException;
import com.gdssoft.core.exception.NotExistException;
import com.gdssoft.core.model.CheckCompleteResult;
import com.gdssoft.framework.model.system.AppFunction;
import com.gdssoft.framework.model.system.AppRole;
import com.gdssoft.framework.model.system.AppUser;
import com.gdssoft.framework.service.system.AppFunctionService;
import com.gdssoft.framework.service.system.AppRoleService;
import com.gdssoft.framework.service.system.AppUserService;

public class AppRoleTestCase extends BaseTestCase{
	@Resource
	private AppRoleService appRoleService;
	
	@Test
	@Rollback(true)
	public void testAppRoleService() {
		AppRole entity = new AppRole();
		//测试检查完整性
		CheckCompleteResult result = entity.checkComplete();
		//必须有检查完整性的结果对象
		TestCase.assertNotNull("check result should't Null", result);
		//因信息不完整，检查结果为假
		TestCase.assertFalse("check result should be False", result.isComplete());
		
		//填充必要信息
		entity.setRoleCode("ROLE_COMMON");
		entity.setRoleName("test role");
		entity.setStatus(Constants.ENABLED);

		//再次检查完整性
		CheckCompleteResult result2 = entity.checkComplete();
		//检查结果应为真
		TestCase.assertTrue("check result should be True", result2.isComplete());
		
		try {
			appRoleService.save(entity);
			TestCase.fail("Should not be successful");
		} catch (NotCompleteException e) {
			TestCase.fail("Should not be an exception");
		} catch (ExistException e) {
			//expected 因RoleCode已存在，新增必然要失败
		} catch (NotExistException e) {
			TestCase.fail("Should not be an exception");
		}
		
		entity.setRoleCode("_ROLE_TEST_");
		AppRole appRole = null;
		try {
			appRole = appRoleService.save(entity);
		} catch (NotCompleteException e) {
			TestCase.fail("Should not be an exception");
		} catch (ExistException e) {
			TestCase.fail("Should not be an exception");
		} catch (NotExistException e) {
			TestCase.fail("Should not be an exception");
		}
		//新增成功，返回结果不为Null
		TestCase.assertNotNull("appRole should't Null", appRole);
		//RoleId不为Null
		TestCase.assertNotNull("appRole should't Null", appRole.getRoleId());
		
		//测试修改一个角色名字
		appRole.setRoleName("testtest");
		try {
			appRole = appRoleService.save(entity);
		} catch (NotCompleteException e) {
			TestCase.fail("Should not be an exception");
		} catch (ExistException e) {
			TestCase.fail("Should not be an exception");
		} catch (NotExistException e) {
			TestCase.fail("Should not be an exception");
		}
		//更新成功，返回结果不为Null
		TestCase.assertNotNull("appRole should't Null", appRole);
		//RoleId不为Null
		TestCase.assertEquals("RoleName should be testtest", 
				"testtest", appRole.getRoleName());
		
		Boolean isRemoved = false;
		try {
			isRemoved = appRoleService.remove(appRole.getRoleId());
		} catch (NotExistException e) {
			TestCase.fail("Should not be an exception");
		}
		//移除成功 返回结果为True
		TestCase.assertTrue("Should be True", isRemoved);
		
		//再执行一次移除动作，这次不可能成功。
		try {
			appRoleService.remove(appRole.getRoleId());
			TestCase.fail("Should not be successful");
		} catch (NotExistException e) {
			//expected 应该是要出现已删除的异常
		}
	}
	
	/*
	//@Test
	public void testMerge(){
		AppRole role=new AppRole();
		role.setRoleId(1L);
		role.setStatus((short)0);
		appRoleService.merge(role);
	}
	
	@Test
	public void updateFunctions() throws Exception{
		AppRole role=appRoleService.get(-1L);
		for(int id=1;id<=2;id++){
			AppFunction appFunction=appFunctionService.get(new Long(id));
			role.getFunctions().add(appFunction);
		}
		appRoleService.save(role);
		
	}
	*/
}
